﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SdmxMl.Panels;

namespace SdmxMl.UC
{
    public partial class UcSearchTree : UserControl
    {
        private TreeView tv;
        public UcSearchTree()
        {
            InitializeComponent();
        }

        public TreeView TreeViewBinded
        {
            get { return tv; }
            set { tv = value; }
        }


        private void textBoxQuickSearch_KeyDown(object sender, KeyEventArgs e)
        {

            if ((e.KeyData & (Keys.Shift | Keys.Alt)) == 0)
            {
                switch (e.KeyData & Keys.KeyCode)
                {
                    case Keys.Return:
                        if ((e.KeyData & Keys.Control) == 0)
                        {
                            buttonSearch_Click(this, EventArgs.Empty);
                        }
                        break;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param> <param name="e"></param>
        private void buttonSearch_Click(object sender, EventArgs e)
        {
            try
            {
                string pattern = textBoxQuickSearch.Text.Trim();

                SearchType st = Search.DefinePattern(ref pattern);

                if (tv != null && tv.Nodes.Count > 0 && pattern.Length > 0)
                {
                    List<TreeNode> list = new List<TreeNode>(10);
                    TreeNode targetNode = null;

                    Search.Process(list, st, radioButtonCode.Checked, null, tv, pattern);
                    if (list.Count == 1)
                        targetNode = list[0];
                    else if (list.Count > 1)
                    {
                        DlgListSearchResult aDlg = new DlgListSearchResult(list);
                        if (aDlg.ShowDialog() == DialogResult.OK)
                        {
                            targetNode = aDlg.SelectedNode;
                        }
                    }

                    if (targetNode != null)
                    {
                        bool ownerDraw = false;
                        if (tv.DrawMode == TreeViewDrawMode.OwnerDrawText)
                        {
                            tv.DrawMode = TreeViewDrawMode.Normal;
                            ownerDraw = true;
                        }

                        tv.BeginUpdate();
                        tv.SelectedNode = targetNode;
                        tv.SelectedNode.EnsureVisible();
                        tv.EndUpdate();
                        if (ownerDraw)
                            tv.DrawMode = TreeViewDrawMode.OwnerDrawText;
                        tv.Focus();
                    }
                    else if (list.Count == 0)
                    {
                        MessageBox.Show("Nothing found or invalid criteria",
                            "Quick Search", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
            catch 
            {
                MessageBox.Show("Sorry. Unable to manage searching.", "Error");
            }

        }

    }
}
